package com.bibrApp.difu.utils;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrValidateUtils {
    // 仅仅包含字母和数字
    private static String LETTER_DIGIT_REGEX = "^[a-z0-9A-Z]+$";
    // 含有数字
    private static String CONTAIN_DIGIT_REGEX = ".*[0-9].*";
    // 包含字母
    private static String CONTAIN_LETTER_REGEX = ".*[a-zA-z].*";

    /**
     * 判断字符串是否仅含有数字和字母
     *
     * @param str
     * @return
     */
    public static boolean isLetterDigit(String str) {
        return str.matches(CONTAIN_DIGIT_REGEX) && str.matches(CONTAIN_LETTER_REGEX);
    }

    /**
     * 替换姓名中的名为*
     * @param str
     * @return
     */
    public static String replaceNameX(String str){
        if(TextUtils.isEmpty(str)){
            return "**";
        }
        String reg = ".{1}";
        StringBuffer sb = new StringBuffer();
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(str);
        int i = 0;
        while(m.find()){
            i++;
            if(i==1)
                continue;
            m.appendReplacement(sb, "*");
        }
        m.appendTail(sb);
        return sb.toString();
    }

    /**
     * 替换所有字符串为*
     * @param str
     * @return
     */
    public static String replaceAllStr(String str){
        if(TextUtils.isEmpty(str)){
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for(int i = 0 ; i < str.length() ; i ++){
            sb.append("*");
        }
        return sb.toString();
    }
}
